// script.js

const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const gridSize = 20;
let snake1 = [{ x: 100, y: 100 }];
let snake2 = [{ x: 700, y: 100 }];
let direction1 = 'RIGHT';
let direction2 = 'LEFT';
let food = { x: Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize, y: Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize };
let score1 = 0;
let score2 = 0;

function drawCell(x, y, color) {
    ctx.fillStyle = color;
    ctx.fillRect(x, y, gridSize, gridSize);
}

function drawSnake(snake, color) {
    snake.forEach(segment => drawCell(segment.x, segment.y, color));
}

function drawFood() {
    drawCell(food.x, food.y, 'red');
}

function updateSnake(snake, direction) {
    const head = { ...snake[0] };

    switch (direction) {
        case 'UP':
            head.y -= gridSize;
            break;
        case 'DOWN':
            head.y += gridSize;
            break;
        case 'LEFT':
            head.x -= gridSize;
            break;
        case 'RIGHT':
            head.x += gridSize;
            break;
    }

    snake.unshift(head);
    if (head.x === food.x && head.y === food.y) {
        score1++; // 假设这是玩家控制的蛇的得分
        food = { x: Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize, y: Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize };
    } else {
        snake.pop();
    }

    // 碰撞检测
    if (head.x < 0 || head.y < 0 || head.x >= canvas.width || head.y >= canvas.height || snake.some(segment => segment !== head && segment.x === head.x && segment.y === head.y)) {
        // 游戏结束逻辑
        alert(`游戏结束！玩家得分：${score1}，电脑得分：${score2}`);
        clearInterval(gameInterval);
    }
}

function moveAI() {
    const head2 = { ...snake2[0] };
    const possibleMoves = ['UP', 'DOWN', 'LEFT', 'RIGHT'];
    let bestMove;

    // 简单的AI逻辑：避免撞墙和自撞，尽量朝食物移动
    possibleMoves.forEach(move => {
        const testSnake = [...snake2];
        const testHead = { ...head2 };

        switch (move) {
            case 'UP':
                testHead.y -= gridSize;
                break;
            case 'DOWN':
                testHead.y += gridSize;
                break;
            case 'LEFT':
                testHead.x -= gridSize;
                break;
            case 'RIGHT':
                testHead.x += gridSize;
                break;
        }

        testSnake.unshift(testHead);

        if (testHead.x >= 0 && testHead.y >= 0 && testHead.x < canvas.width && testHead.y < canvas.height && !testSnake.some(segment => segment !== testHead && segment.x === testHead.x && segment.y === testHead.y)) {
            if (!bestMove || Math.hypot(testHead.x - food.x, testHead.y - food.y) < Math.hypot(head2.x - food.x, head2.y - food.y)) {
                bestMove = move;
            }
        }
    });

    direction2 = bestMove || direction2; // 如果没有找到更好的移动方向，则保持当前方向
    updateSnake(snake2, direction2);
}

function update() {
    updateSnake(snake1, direction1);
    moveAI();

    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawSnake(snake1, 'green');
    drawSnake(snake2, 'blue');
    drawFood();
}

canvas.addEventListener('mousemove', (event) => {
    const rect = canvas.getBoundingClientRect();
    const x = event.clientX - rect.left;
    const y = event.clientY - rect.top;

    if (x < gridSize * 2) direction1 = 'LEFT';
    else if (x > canvas.width - gridSize * 2) direction1 = 'RIGHT';
    else if (y < gridSize * 2) direction1 = 'UP';
    else if (y > canvas.height - gridSize * 2) direction1 = 'DOWN';
});

const gameInterval = setInterval(update, 100);